package com.selen.mapper;

import com.selen.entities.Room;
import com.selen.entities.School;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Selen
 */
@Repository
public interface RoomMapper {

    @Insert("insert into ums_room " +
            "(`title`,  `address`, `info`, `capacity`, `deleted`, `created`, `updated`) " +
            "values (#{title}, #{address}, #{info}, #{capacity}, #{deleted}, #{created}, #{updated})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Room room);

    @Select("select ur.id, ur.title, ur.address, ur.capacity, " +
            "ur.info, ur.deleted, ur.created, ur.updated " +
            "from ums_room as ur " +
            "where ur.id = #{param1} and ur.deleted = 0")
    Room selectById(Long id);

    @Update("<script>" +
            "update ums_room " +
            "<set> " +
            "<if test = 'title != null'> title = #{title}, </if> " +
            "<if test = 'address != null'> address = #{address}, </if> " +
            "<if test = 'info != null'> info = #{info}, </if> " +
            "<if test = 'capacity != null'> capacity = #{capacity}, </if> " +
            "<if test = 'deleted != null'> deleted = #{deleted}, </if> " +
            "<if test = 'created != null'> created = #{created}, </if> " +
            "<if test = 'updated != null'> updated = #{updated}, </if> " +
            " </set> " +
            "where id = #{id} and deleted = 0 " +
            "</script>")
    int updateById(Room room);

    @Update("update ums_room set deleted = 1, updated = now() " +
            "where id = #{param1}")
    int logicDeleteById(Long id);

    @Update("<script>" +
            "update ums_room set deleted = 1, updated = now() " +
            "where " +
            "<foreach collection = 'list' item = 'e' open = 'id in (' close = ')' separator = ',' > " +
            "${e}" +
            " </foreach>" +
            "</script>")
    int logicDeleteByIds(List<Long> ids);

    @Select("select ur.id, ur.title, ur.address, ur.info, ur.capacity, ur.deleted, ur.created, ur.updated " +
            "from ums_room as ur " +
            "where ur.deleted = 0 ")
    List<Room> selectAll();

    @Delete("delete from ums_room where id = #{param1}")
    int deleteById(Long id);

    @Select("select ur.id, ur.title, ur.address, ur.info, ur.capacity, ur.deleted, ur.created, ur.updated " +
            "from ums_room as ur " +
            "where ur.title like concat('%', #{param1}, '%') and ur.deleted = 0")
    List<Room> selectByDimName(String keyword);


}
